//
//  PhoneRechargeViewController.swift
//  Entrance
//
//  Created Chunhui on 2018/1/31.
//  Copyright © 2018年 chunhuiLai. All rights reserved.
//

import UIKit
import SYKit
import RxSwift
import RxCocoa
import RxDataSources
import SnapKit

class PhoneRechargeFlowLayout: UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        
        minimumLineSpacing = 20
        minimumInteritemSpacing = 20
        
        sectionInset = UIEdgeInsets.init(top: 20, left: minimumInteritemSpacing, bottom: 20, right: minimumInteritemSpacing)
        let columns: CGFloat = SYRuler.iPhoneHorizontal(2, 3, 3).value
        let itemW = (UIScreen.sy_width - (CGFloat(1) + columns) * minimumInteritemSpacing) / columns
        itemSize =  CGSize.init(width: itemW, height: 80)
        
        headerReferenceSize = CGSize(width: UIScreen.sy_width, height: 30)
        
        scrollDirection = .vertical
    }
}

class PhoneRechargeViewController: BaseViewController, UICollectionViewDelegate {
    // Reference to the Presenter's interface.
    var presenter: PhoneRechargePresentation?
    
    lazy var headerView: PhoneRechargeHeaderView = {
        let header = PhoneRechargeHeaderView()
        header.backgroundColor = UIColor.green
        return header
    }()
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: PhoneRechargeFlowLayout())
        collectionView.backgroundColor = UIColor.white
        collectionView.register(PhoneRechargeCell.self, forCellWithReuseIdentifier: PhoneRechargeCell.defaultReuseId)
        collectionView.register(PhoneRechargeSectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: PhoneRechargeSectionHeaderView.defaultReuseId)
        
        return collectionView
    }()
    
    let sections = Variable([PhoneRechargeSection]())
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        layoutUI()
        
        let entity1 = PhoneRechargeEntity.init(id: "1", price: "30")
        let entity2 = PhoneRechargeEntity.init(id: "2", price: "30")
        let entity3 = PhoneRechargeEntity.init(id: "3", price: "30")
        let entity4 = PhoneRechargeEntity.init(id: "4", price: "30")
    
        sections.value = [
            PhoneRechargeSection.init(model: "recharge", items: [
                                                        entity1,
                                                        entity2,
                                                        entity3,
                                                        entity4
                                                                ]),
            PhoneRechargeSection.init(model: "fjongong", items: [
                                                        entity1,
                                                        entity2,
                                                        entity3,
                                                                ])
            ]
        
        let (configureCollectionViewCell, configureSupplementaryView) = PhoneRechargeViewController.collectionViewDataSourceUI()

        let dataSource = RxCollectionViewSectionedAnimatedDataSource(
            configureCell: configureCollectionViewCell,
            configureSupplementaryView: configureSupplementaryView
        )
        
        self.sections.asObservable()
            .bind(to: collectionView.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)

        
        collectionView.rx
            .itemSelected
            .map { indexPath in
                return (indexPath, dataSource[indexPath])
            }
            .subscribe({ element in
                printlog(element)
            })
            .disposed(by: disposeBag)
        
        collectionView.rx
            .setDelegate(self)
            .disposed(by: disposeBag)
        
    }
    
    func layoutUI() {
        view.addSubview(headerView)
        view.addSubview(collectionView)
        
        var topContraint: CGFloat = 0
        if let nav = navigationController, !nav.isNavigationBarHidden {
            topContraint = nav.navigationBar.frame.maxY
        }

        headerView.snp.makeConstraints { (make) in
            make.top.equalTo(topContraint)
            make.left.right.equalToSuperview()
            make.height.equalTo(80)
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.bottom)
            make.left.bottom.right.equalToSuperview()
        }
    }
}

//MARK: PhoneRechargeInterface
extension PhoneRechargeViewController: PhoneRechargeInterface {
}


//MARK: setup PhoneRechargecell UI
extension PhoneRechargeViewController {
    static func collectionViewDataSourceUI() -> (
        CollectionViewSectionedDataSource<PhoneRechargeSection>.ConfigureCell,
        CollectionViewSectionedDataSource<PhoneRechargeSection>.ConfigureSupplementaryView
        ) {
            return (
                { (_, cv, indexPath, element) in
                    let cell = cv.dequeueReusableCell(withReuseIdentifier: PhoneRechargeCell.defaultReuseId, for: indexPath) as! PhoneRechargeCell
                   
                    cell.worthLabel.text = element.worth
                    cell.payPriceLabel.text = element.price
                    return cell
            },
                { (ds ,cv, kind, indexPath) in
                    let section = cv.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: PhoneRechargeSectionHeaderView.defaultReuseId, for: indexPath) as! PhoneRechargeSectionHeaderView

                    section.titleLabel.text = ds[indexPath.section].model
                    return section
            }
            )
    }
}



